from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


# 基类，封装常用的操作方法
class BasePage:
    def __init__(self, driver):
        self.driver = driver

    def find_element(self, by, value):
        return WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((by, value))
        )

    def click_element(self, by, value):
        element = self.find_element(by, value)
        element.click()


# 好友列表页面
class FriendListPage(BasePage):
    def __init__(self, driver):
        super().__init__(driver)
        self.friend_item = (By.CSS_SELECTOR, ".friend-item")
        self.delete_button = (By.CSS_SELECTOR, ".delete-button")

    def select_friend(self):
        friends = self.driver.find_elements(*self.friend_item)
        if friends:
            friends[0].click()
        return self

    def click_delete_button(self):
        self.click_element(*self.delete_button)
        return ConfirmDeletePage(self.driver)


# 确认删除页面
class ConfirmDeletePage(BasePage):
    def __init__(self, driver):
        super().__init__(driver)
        self.confirm_button = (By.CSS_SELECTOR, ".confirm-button")

    def confirm_delete(self):
        self.click_element(*self.confirm_button)


# 测试用例
# def test_delete_friend():
#     driver = webdriver.Chrome()
#     try:
        # 导入必要的模块
        from time import sleep  # 用于添加等待时间
        from appium import webdriver  # Appium的Python客户端库
        from appium.options.android import UiAutomator2Options  # Android UiAutomator2驱动选项
        from selenium.webdriver import ActionChains
        from selenium.webdriver.common.actions.action_builder import ActionBuilder
        from selenium.webdriver.common.actions.pointer_input import PointerInput
        from selenium.webdriver.common.by import By

        # 配置Appium连接选项
        options = UiAutomator2Options()  # 创建UiAutomator2选项对象

        # 设置基本设备能力(Desired Capabilities)
        options.platform_name = "Android"  # 指定平台为Android
        options.automation_name = "UiAutomator2"  # 使用UiAutomator2自动化引擎
        options.device_name = "emulator-5554"  # 指定设备名称(adb devices获取)
        # options.app_package = "com.android.settings"  # 要测试的App包名
        # options.app_activity = ".Settings"  # 要启动的Activity名称
        options.new_command_timeout = 300  # 新命令超时时间(秒)
        options.udid = "emulator-5554"  # 明确指定设备UDID

        # 初始化Appium WebDriver
        # 参数1: Appium服务器地址(默认本地4723端口)
        # 参数2: 配置选项
        driver = webdriver.Remote(
            command_executor='http://127.0.0.1:4723',  # Appium服务器地址
            options=options  # 传入配置选项
        )

        # 连接成功提示
        print("连接成功！")  # 打印连接成功信息

        # 等待3秒(模拟操作间隔)
        sleep(3)  # 暂停3秒，确保应用完全启动
        # 第一步 初始化 ActionChains
        actions = ActionChains(driver)

        # 第二步 定义触摸输入源
        touch = PointerInput("touch", "finger")
        # 第三步 添加动作到动作链
        actions.w3c_actions = ActionBuilder(driver, mouse=touch)

        driver.find_element(By.XPATH, "//*[@text='QQ']").click()
        driver.swipe(100, 100, 100, 400, 200)
        driver.find_element(By.XPATH, "//*[@text='联系人']").click()
        driver.swipe(1700,1800,1700,1800,1860)
        driver.find_element(By.CLASS_NAME, "android.widget.LinearLayout").click()
        driver.find_element(By.ID,"com.tencent.mobileqq.profilecard_feature_impl:id/cfg").click()
        driver.swipe(100,100,100,400,200)
        driver.find_element(By.XPATH,"//*[@text='删除好友']")

        # 第四步 执行动作
        actions.perform()
        driver.quit()
    #     friend_list_page = FriendListPage(driver)
    #     friend_list_page.select_friend().click_delete_button().confirm_delete()
    #
        print("好友删除成功")
    # except Exception as e:
    #     print(f"删除好友时出现错误: {e}")
    # finally:
    #     driver.quit()


# if __name__ == "__main__":
#     test_delete_friend()
